﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml;
using System.Xml.Serialization;
using JsonServer.Interfaces;
using log4net;

namespace AshxWebApp01
{
    public class Global : System.Web.HttpApplication
    {
        #region Logging
        protected static readonly ILog _log = LogManager.GetLogger(typeof(Global));
        #endregion

        #region Constants
        public const string JSON_SERVERS = "JSON_SERVERS";
        #endregion

        #region State
        #endregion

        protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

            try
            {
                var jsonServersCfg = ConfigurationManager.AppSettings[JSON_SERVERS];
                var doc = new XmlDocument();
                doc.Load(jsonServersCfg);

                var ser = new XmlSerializer(typeof(JsonServersConfig));
                var cfg = (JsonServersConfig)ser.Deserialize(new XmlNodeReader(doc.DocumentElement));

                var servers = new Dictionary<string, JsonServerApi>();

                foreach(var server in cfg.JsonServerConfig)
                {
                    try
                    {
                        var iJsonServer = (IJsonServer)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(server.Assembly, server.Type);

                        iJsonServer.Configure(null);
                        var api = new JsonServerApi();

                        iJsonServer.RegisterApi(api);

                        servers.Add(server.Name, api);
                    }
                    catch(TypeLoadException excp)
                    {
                        var msg = string.Format("Missing dependency encountered while loading server: [{0}].  Assembly: [{1}] Type: [{2}]", 
                                                server.Name,
                                                server.Assembly,
                                                server.Type);
                        _log.Error(msg, excp);        
                    }
                    catch (Exception excp)
                    {
                        throw;
                    }

                }

                Application[JSON_SERVERS] = servers;
            }
            catch (Exception excp)
            {
                _log.Fatal("Fatal Exception on startup", excp);
                throw;
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}